package com.wiz.web.controllor;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.service.PRConfigService;
import com.wiz.to.PRTO;
import com.wiz.vo.ResponResultVO;

/**
 * 配置油品
 * @author xxy
 *
 */
@Controller
@RequestMapping("/prconfig")
public class PRConfigContollor {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private PRConfigService prconfigService;
	
	
	@RequestMapping(value="/toPRConfig")
	public String toPRConfig(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "hqId", required = true) String hqId,
			HttpSession session, Model model){
		List<PRTO>list =  prconfigService.queryAllPR();
		
		model.addAttribute("list", list);
		model.addAttribute("hqId", hqId);
		
		return "hq/deployPR";
	}
	@RequestMapping(value="/listPRConfigByHqId")
	@ResponseBody
	public String listPRConfigByHqId(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "hqId", required = true) String hqId,
			HttpSession session, Model model){
		List<Integer>ids = prconfigService.listPRConfigByHqId(hqId);
		
		ResponResultVO result = new ResponResultVO(200, "success", ids, 0, 0, 0);
		return gson.toJson(result);
	}
	
	@RequestMapping(value="/modify")
	@ResponseBody
	public String modify(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "hqId", required = true) String hqId,
			@RequestParam(value = "prIds", required = true) String prIds,
			HttpSession session, Model model){
		ResponResultVO result = null;
		try {
			prconfigService.modify(hqId, prIds);
			result = new ResponResultVO(200, "success", new ArrayList<String>(), 0, 0, 0);
		} catch (Exception e) {
			e.printStackTrace();
			result = new ResponResultVO(200, e.getMessage(), new ArrayList<String>(), 0, 0, 0);
		}
		return gson.toJson(result);
	}
}
